<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 15:22
 */

namespace app\admin\model;


use think\Model;

class CsdList extends Model
{
    /*
     * 列表
     */
    public function getorderist($pageNum, $numPerPage,$param,$company_id) {
        $field[] = 'dl.id';
        $field[] = 'o.order_sn';
        $field[] = 'dl.update_time';
        $field[] = 'dl.license_number';
        $field[] = 'o.license_type';
        $field[] = 'dl.license_typename';
        $field[] = 'dl.user_name';
        $field[] = 'dl.license_nature';
        $field[] = 'dl.vehicle_brand';
        $field[] = 'dl.license_register_date';
        $field[] = 'dl.license_grant_date';
        $field[] = 'dl.vehicle_identification';
        // $field[] = 'dl.copy_license_number';
        // $field[] = 'dl.files_number';
        // $field[] = 'dl.inspection_record';
        $field[] = 'o.mobile';
        $field[] = 'o.license_type';
        $field[] = 'o.open_id';
        $list = db("order")
                    ->alias('o')
                    ->field($field)
                    ->join('driving_license dl','o.order_id=dl.order_id','RIGHT')
                    // ->where('is_delete',0)
                    ->where('company_id',$company_id)
                    ->where(function($query) use ($param){
                        if(!empty($param['license_number'])){
                            $query->where('dl.license_number','like','%'.$param['license_number'].'%');
                        }
                        if(!empty($param['license_type'])){
                            $query->where('o.license_type','eq',$param['license_type']);
                        }
                        $query->where('o.mobile','like','%'.$param['mobile'].'%');
                        $query->where('dl.user_name','like','%'.$param['user_name'].'%');
                    })
                    ->group('dl.vehicle_identification')
                    ->order('dl.create_time desc')
                    ->page($pageNum, $numPerPage)
                    ->select();
        return $list;
    }
    /*
     * 数量
     */
    public function getorderount($param,$company_id) {
        $count = db("order")
                ->alias('o')
                ->join('driving_license dl','o.order_id=dl.order_id','RIGHT')
                // ->where('is_delete',0)
                ->where('company_id',$company_id)
                ->where(function($query) use ($param){
                    if(!empty($param['license_number'])){
                        $query->where('dl.license_number','%'.$param['license_number'].'%');
                    }
                    if(!empty($param['license_type'])){
                            $query->where('o.license_type','eq',$param['license_type']);
                    }
                    $query->where('o.mobile','like','%'.$param['mobile'].'%');
                        $query->where('dl.user_name','like','%'.$param['user_name'].'%');
                })
                ->group('dl.vehicle_identification')
                ->count();
        return $count;
    }
}